package com.prs.crm.action.hr.report;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Leave;
import com.prs.crm.domain.hr.LeaveType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.hr.LeaveService;

public class LeaveCountAction extends ReportAction {

	private static final long serialVersionUID = 2828873721706755432L;

	private ChartService chartService;
	private PaginationSupport pagination;
	private Leave leave;
	private ChartResult chartResult;
	private Person person;

	private Date datestart;
	private Date dateend;
	private String acceptnames;
	private String accepts;
	private String category;

	private LeaveType type;
	private LeaveService leaveService;
	private List<LeaveType> types;

	@Action("listLeaveCount")
	public String listLeaveCount() {
		types = this.getLeaveService().getAllLeaveType();
		return SUCCESS;
	}

	@Action(value = "viewPie", results = {
			@Result(name = "success", type = "chain", location = "listLeaveCount"),
			@Result(name = "error", type = "chain", location = "listLeaveCount") })
	public String viewPie() {
		if (type == null) {
			addActionError("请假类型不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (acceptnames == null || "".equals(acceptnames)) {
			addActionError("人员不能为空");
			return ERROR;
		}
		Collection<Object[]> leaveList = this.getLeaveService().findByType(
				this.getType(),
				this.getDatestart(),
				this.getDateend(),
				!accepts.trim().equals("") ? this.getLeaveService()
						.convertIdsToPersons(
								ToolKits.convertStringAryToIntAry(accepts
										.split(","))) : null);
		chartService.setWidth(650);
		chartResult = chartService.export(this.getLeaveService()
				.fillPieDataset(leaveList), "请假统计分析", "leaveInfoAction");
		return SUCCESS;
	}

	@Action(value = "viewBar", results = {
			@Result(name = "success", type = "chain", location = "listLeaveCount"),
			@Result(name = "error", type = "chain", location = "listLeaveCount") })
	public String viewBarChart() {
		if (type == null) {
			addActionError("请假类型不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (acceptnames == null || "".equals(acceptnames)) {
			addActionError("人员不能为空");
			return ERROR;
		}
		Collection<Object[]> leaveList = this.getLeaveService().findByType(
				this.getType(),
				this.getDatestart(),
				this.getDateend(),
				!accepts.trim().equals("") ? this.getLeaveService()
						.convertIdsToPersons(
								ToolKits.convertStringAryToIntAry(accepts
										.split(","))) : null);
		chartService.setWidth(650);
		chartResult = chartService
				.export(this.getLeaveService().fillBarDataset(leaveList),
						"请假分析", "次数", "", "leaveInfoAction");
		return SUCCESS;
	}

	@Action("leaveInfoAction")
	public String leaveInfoAction() {
		person = this.getLeaveService().getPerson(this.getCategory());
		DetachedCriteria criteria = this.getLeaveService().findByPage(
				this.getPaginationHelper().createQuery(Leave.class), type,
				this.getDatestart(), this.getDateend(), person);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLeaveService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportLeaveReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String reportLeave() {
		ReportTable table = new ReportTable("员工请假报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("类型", "type.name"));
		table.addCell(new ReportCell("请假时间", "recordTime"));
		table.addCell(new ReportCell("请假人", "recorder.resume.name"));
		table.addCell(new ReportCell("开始时间", "beginTime"));
		table.addCell(new ReportCell("结束时间", "endTime"));
		table.addCell(new ReportCell("状态", "state", Constrains
				.getReportLeaveStatus()));

		DetachedCriteria criteria = this.getLeaveService().findByPage(
				this.getPaginationHelper().createQuery(Leave.class), type,
				this.getDatestart(), this.getDateend(), person);
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public Leave getLeave() {
		return leave;
	}

	public void setLeave(Leave leave) {
		this.leave = leave;
	}

	public List<LeaveType> getTypes() {
		return types;
	}

	public Date getDatestart() {
		return datestart;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

	public void setTypes(List<LeaveType> types) {
		this.types = types;
	}

	public String getAcceptnames() {
		return acceptnames;
	}

	public void setAcceptnames(String acceptnames) {
		this.acceptnames = acceptnames;
	}

	public String getAccepts() {
		return accepts;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public void setType(LeaveType type) {
		this.type = type;
	}

	public LeaveType getType() {
		return type;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
}
